package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lanju.entity.Certificate;
import com.lanju.entity.UserIdCard;

@Repository
public class CertificateDao {

    @PersistenceContext(unitName = "financialmarketingUnit")
    private EntityManager entityManager;
    
    @Autowired
   	SessionFactory sessionFactory;
    
    
	public void saveCertificate(Certificate obj){
		try {
			entityManager.persist(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	public void updateCertificate(Certificate obj){
		try {
			entityManager.merge(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	public List<Certificate> getAllCertificateList(String houseId,String type){
		try {
			StringBuffer sql=new StringBuffer();
			sql.append("from Certificate where houseId='"+houseId+"' and type in ("+type+")");
			Query query=entityManager.createQuery(sql.toString());
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Certificate getCertificate(String houseId,String type){
		try {
			StringBuffer sql=new StringBuffer();
			sql.append("from Certificate where houseId='"+houseId+"' and type='"+type+"'");
			Query query=entityManager.createQuery(sql.toString());
			List list=query.getResultList();
			if(list !=null && list.size()>0){
 				return (Certificate)list.get(0);
			}else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int delCertificate(Map<String,Object> map){
		try {
			StringBuffer sql=new StringBuffer();
			sql.append("delete from Certificate where houseId='"+map.get("houseId").toString()+"' and type='"+map.get("type").toString()+"' and url in("+map.get("url").toString()+")");
			this.sessionFactory.getCurrentSession().createQuery(sql.toString()).executeUpdate();
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public void addIdCard(UserIdCard ui){
		try {
			entityManager.persist(ui);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updatedCard(UserIdCard ui){
		try {
			entityManager.merge(ui);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Transactional
	public void delCard(Map<String,Object> map) {
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("delete from UserIdCard where imgUrl in("+map.get("imgUrl").toString()+") and idcardUrlId='"+map.get("userId").toString()+"'");
			entityManager.createQuery(sql.toString());
			this.sessionFactory.getCurrentSession().createQuery(sql.toString()).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
